/*
 *  Copyright 2011-2012 Redia A/S
 * 
 *  This file is part of Redias Android Utilities.
 *
 *  Redias Android Utilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redias Android Utilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redias Android Utilities.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.lib.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.view.Display;
import android.view.WindowManager;
import dk.redia.lib.xmlrpc.InfoGalleriXmlRpcClientCall;

public class ErrorReport {
	private String extra;
	private String autoExtras;
	private String description;
	private String email;
	private String defaultMessage;
	private Throwable exception;
	
	public ErrorReport() {
		
		WindowManager wm = (WindowManager)AppApplication.getAppContext().getSystemService(Context.WINDOW_SERVICE);
		Display d = null;
		ConnectivityManager cm = null;
		if (wm!=null) {
			d = wm.getDefaultDisplay();
		}
		
		cm = (ConnectivityManager) AppApplication.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		
		autoExtras = "Automatic extras:\n";
		autoExtras += "Customer id: " + AppConfiguration.getInstance().getCustomerId() + "\n";
		autoExtras += "Gallery id: " + AppConfiguration.getInstance().getGalleryId() + "\n";
		autoExtras += "App version: " + VersionUtils.getVersion() + "(" + VersionUtils.getVersionNickName() + ")\n";
		if (cm!=null) {
			autoExtras += "Network type: " + cm.getActiveNetworkInfo().getTypeName() + "\n";
		}
		autoExtras += "Device IP addresses: " + getLocalIpAddresses() + "\n";
		if (d!=null) {
			autoExtras += "Display size: " + d.getWidth() + "x" + d.getHeight() + "\n";
		}
		autoExtras += "InfoGalleri XML-RPC hostname: " + new InfoGalleriXmlRpcClientCall().url  + "\n";
		autoExtras += "Phone model: " + Build.MODEL + "\n";
		autoExtras += "Phone manufacturer: " + Build.MANUFACTURER + "\n";
		autoExtras += "\n";
	}
	
	public String getExtra() {
		return extra +autoExtras;
	}
	public void setExtra(String extra) {
		this.extra = extra;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getDefaultMessage() {
		return defaultMessage;
	}
	public void setDefaultMessage(String defaultMessage) {
		this.defaultMessage = defaultMessage;
	}
	public Throwable getException() {
		return exception;
	}
	public void setException(Throwable exception) {
		this.exception = exception;
	}
	
	private String getLocalIpAddresses() {
		String result = "";
	    try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()) {
	                	result += inetAddress.getHostAddress().toString() + ", ";
	                }
	            }
	        }
	    } catch (SocketException ex) {
	    	ex.printStackTrace();
	    }
	    return result;
	}
}
